package controll;

import entity.Business;
import Service.BusinessService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

@RestController
@RequestMapping("/business")
public class BusinessController {

    @Resource
    private BusinessService businessService;

    // 1. 创建业务
    @PostMapping("/create")
    public ResponseEntity<String> addBusiness(@RequestBody Business business) {
        try {
            businessService.addBusiness(business);
            return ResponseEntity.ok("业务创建成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("系统异常：" + e.getMessage());
        }
    }

    // 2. 修改业务
    @PutMapping("/update")
    public ResponseEntity<String> updateBusiness(@RequestBody Business business) {
        try {
            businessService.updateBusiness(business);
            return ResponseEntity.ok("业务修改成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("系统异常：" + e.getMessage());
        }
    }

    // 3. 删除业务
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteBusiness(@PathVariable String id) {
        try {
            businessService.deleteBusiness(id);
            return ResponseEntity.ok("业务删除成功");
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("系统异常：" + e.getMessage());
        }
    }

    // 4. 查询业务详情
    @GetMapping("/{id}")
    public ResponseEntity<Business> getBusiness(@PathVariable String id) {
        try {
            Business business = businessService.getBusiness(id);
            return ResponseEntity.ok(business);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(null);
        }
    }

    // 5. 导出业务到 Excel
    @GetMapping("/export")
    public void exportBusiness(HttpServletResponse response) throws IOException {
        try {
            businessService.exportBusiness(response);
        } catch (IllegalArgumentException e) {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"message\":\"" + e.getMessage() + "\"}");
        } catch (Exception e) {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"message\":\"系统异常：" + e.getMessage() + "\"}");
        }
    }

    // 6. 从 Excel 导入业务
    @PostMapping("/import")
    public ResponseEntity<?> importCustomer(@RequestParam("file") MultipartFile file) {
        try {
            businessService.addBusiness((Business) file);
            return ResponseEntity.ok().body("客户数据导入成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("导入失败：" + e.getMessage());
        }
    }
}